package com.kusiri.kframework.component.library.table
{
	import com.kusiri.kframework.event.KfValueObjectEvent;
	import com.kusiri.kframework.model.IKfValueObject;
	import com.kusiri.kframework.model.IKfValueObjectList;
	import com.kusiri.kframework.model.KfValueObjectListChangedEvent;
	import com.kusiri.kframework.model.KfValueObjectSparsePropertyMap;
	import com.kusiri.kframework.ui.KfUiComponent;
	
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.IFlexDisplayObject;

	public class TableComponent extends KfUiComponent 
	{
		private var self : TableComponent;
		
		
		/**
		 * This is the configuration
		 * These properties will be available to inspect in the UI builder
		 */ 
	
		public var valueObjectList : IKfValueObjectList;
		
		
		public var valueObjectSparsePropertyMap : KfValueObjectSparsePropertyMap;
		
		/**
		 * This is the UIComponent, set in the constructor
		 */
		private var _uiComponent : TableComponentUi; 
	
		/**
		 * Standard UI Component stuff
		 */		

		/**
		 * Set up the ui component
		 */
		public function TableComponent() : void
		{
			self = this;
			
			_uiComponent = new TableComponentUi();
			_uiComponent.controller = this;
		}

		/**
		 * Get the Flex UI Component
		 */
		override public function get __uiComponent() : IFlexDisplayObject
		{
			return _uiComponent;
		}

		/**
		 * Remove event listeners, etc.
		 */
		override public function __deactivate() : void
		{
			valueObjectList.removeEventListener(KfValueObjectListChangedEvent.EVENT_NAME,onListChange);
		}

		private function onListChange(event : KfValueObjectListChangedEvent) : void
		{
			if (event.inserted.length > 0 || event.removed.length > 0)
			{
				_uiComponent.dataProvider = valueObjectList.valueObjects;
			}
		}

		/**
		 * Tell the UI Component to load its data.
		 */
		override public function __initializeComponent(onSuccess : Function, onFail : Function) : void
		{
			
			var columns : Array = [];
			
			var valueObjectSparsePropertyMap : Object = { Id : 'Identifier', Subject: 'Name' };
			
			for ( var prop : String in valueObjectSparsePropertyMap)
			{
				var col : DataGridColumn = new DataGridColumn();
				col.dataField = prop;
				col.headerText = valueObjectSparsePropertyMap[prop];
				columns.push(col);
			}
			
			_uiComponent.columns = columns;
			
			if (!valueObjectList)
			{
				onSuccess(this);
				return;	
			} 
			
			// very simple update - redraw whole table if anything add / removed
			// individual rows will get updated by data binding
			valueObjectList.addEventListener(KfValueObjectListChangedEvent.EVENT_NAME,onListChange);
			
			valueObjectList.__initializeComponent(
				function(valueObjectSet:IKfValueObjectList) : void
				{
					_uiComponent.dataProvider = valueObjectSet.valueObjects;
					onSuccess(self);
				},
				function(error:*) : void
				{
					onFail(self, error);
				}
			);
		}
		
		public function onRowDoubleClick(valueObject : IKfValueObject) : void
		{
			var event : KfValueObjectEvent = new KfValueObjectEvent();
			event.valueObject = valueObject;
			event.__name = 'rowDoubleClick';			
			__eventActions.raiseEvent(this, event);
			
		}
		
	}
}